import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/const/CacheConst.dart';
import 'package:mahjong/entity/mahjong/MahjongInfoEntity.dart';
import 'package:mahjong/entity/UserEntityInfo.dart';
import 'package:mahjong/page/GameIndex.dart';
import 'package:mahjong/notify/CurrentUserInfo.dart';
import 'package:provider/provider.dart';
import 'package:mahjong/entity/user/MahjongUserInfo.dart';
import 'package:mahjong/notify/LeftUserInfo.dart';
import 'package:mahjong/notify/TopUserInfo.dart';
import 'package:mahjong/notify/RightUserInfo.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/util/CustomNavigatorObserver.dart';
import 'package:mahjong/page/UserLoginWidget.dart';
import 'package:mahjong/entity/mahjong/DissolutionEntity.dart';
import 'package:mahjong/util/CacheUtil.dart';

void main() {
  asyncInit();
}

void asyncInit() async {
  WidgetsFlutterBinding.ensureInitialized();
  /*await OrientationPlugin.setEnabledSystemUIOverlays([]);
  await OrientationPlugin.setPreferredOrientations(
      [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]); */
  CacheUtil.getInstance(() {
    Widget userCurrenPage;
    UserEntityInfo userInfoEntity = UserEntityInfo();
    String userInfoStr =
        CacheUtil.preferencesInstance.getString(CacheConst.userInfo);
    if (userInfoStr == null) {
      userCurrenPage = UserLoginWidget();
    } else {
      userInfoEntity = UserEntityInfo.fromJson(Map.castFrom(json.decode(
          CacheUtil.preferencesInstance.getString(CacheConst.userInfo))));
      if (userInfoEntity != null) {
        userCurrenPage = GameIndex();
      }
    }
    // 1334, 750
    runApp(ScreenUtilInit(
        designSize: Size(812, 375),
        builder: () => MultiProvider(
              providers: [
                ChangeNotifierProvider(
                    create: (_) => MahjongInfoEntity(state: 0)),
                ChangeNotifierProvider(create: (_) => CurrentUserInfo(null)),
                ChangeNotifierProvider(create: (_) => LeftUserInfo(null)),
                ChangeNotifierProvider(create: (_) => RightUserInfo(null)),
                ChangeNotifierProvider(create: (_) => TopUserInfo(null)),
                ChangeNotifierProvider(create: (_) => MahjongUserInfo()),
                ChangeNotifierProvider(create: (_) => userInfoEntity),
                ChangeNotifierProvider(create: (_) => DissolutionEntity()),
              ],
              child: MaterialApp(
                localizationsDelegates: [
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate
                ],
                locale: const Locale("en", "US"),
                supportedLocales: [
                  const Locale("zh", "CH"), //CH和CN好像都行
                  const Locale("en", "US")
                ],
                // debugShowCheckedModeBanner: false,
                navigatorKey: navigatorKey,
                navigatorObservers: [
                  CustomNavigatorObserver.getInstance(),
                  BotToastNavigatorObserver()
                ],
                builder: BotToastInit(),
                home: userCurrenPage,
              ),
            )));
  });
}
